package com.tbit.uqbike.webmanager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.object.pojo.OperaPsw;
import com.tbit.uqbike.object.pojo.dto.OperaPswDTO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * 操作密钥控制器
 */
@Validated
@RestController
@RequestMapping("/operaPsw")
public class OperaPswController {

    @Autowired
    private OperaPswService operaPswService;

    /**
     * 操作密钥分页查询
     *
     * @param brandId  区域id
     * @param pageNo   页码
     * @param pageSize 条数
     */
    @GetMapping("/queryPage")
    public JsonResult queryPage(@NotNull Integer brandId, @NotNull Integer pageNo, @NotNull Integer pageSize) {
        Page<OperaPsw> operaPswList = operaPswService.queryPage(brandId, pageNo, pageSize);
        return JsonResult.ok(operaPswList.getRecords(), (int) operaPswList.getTotal());
    }

    /**
     * 根据id查询操作密钥
     *
     * @param operaPswId 密钥id
     */
    @GetMapping("/getByOperaPswId")
    public Result<OperaPsw> getByOperaPswId(@NotNull Integer operaPswId) {
        OperaPsw operaPsw = operaPswService.getByOperaPswId(operaPswId);
        return Result.success(operaPsw);
    }

    /**
     * 根据模块key查询操作密钥
     *
     * @param brandId 区域id
     * @param module  模块key
     */
    @GetMapping("/getByModule")
    public Result<OperaPsw> getByModule(@NotNull Integer brandId, @NotNull Integer module) {
        OperaPsw operaPsw = operaPswService.getOperaPswByModule(brandId, module);
        return Result.success(operaPsw);
    }

    /**
     * 根据id修改操作密钥
     *
     * @param operaPswDTO .
     */
    @PostMapping("/updateByOperaPswId")
    @OperateLog(module = ModuleEnum.OPERA_PSW, methods = MethodEnum.OPERA_PSW_UPDATE)
    public Result<String> updateByOperaPswId(OperaPswDTO operaPswDTO) {
        LoginInfo login = BaseController.getLogin(ServletUtils.getRequest());
        operaPswDTO.setOperaUserId(login.getAccountUserId());
        operaPswService.updateByOperaPswId(operaPswDTO);
        return Result.success();
    }
}
